package cn.daniellee.plugin.ce.listener;

import cn.daniellee.plugin.ce.CraftEconomy;
import cn.daniellee.plugin.ce.model.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class PlayerListener implements Listener {

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent e) {
		Player player = e.getPlayer();
		String identity = CraftEconomy.getInstance().isOnlineMode() ? player.getUniqueId().toString() : player.getName();
		if (identity != null && !identity.equals("")) {
			PlayerData playerData = CraftEconomy.getInstance().getStorage().refreshPlayerData(identity);
			if (playerData != null) {
				if (CraftEconomy.getInstance().isOnlineMode()) {
					if (playerData.getName() == null) {
						CraftEconomy.getInstance().getStorage().updatePlayerData(player.getUniqueId().toString(), "name", player.getName());
					}
				} else {
					if (playerData.getUuid() == null) {
						CraftEconomy.getInstance().getStorage().updatePlayerData(player.getName(), "uuid", player.getUniqueId().toString());
					}
				}
			}
		}
	}

}
